package com.woniuxy.woniuhis.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Room;
import com.woniuxy.woniuhis.exception.RoomServiceException;
import com.woniuxy.woniuhis.mapper.RoomMapper;
import com.woniuxy.woniuhis.service.RoomService;
import com.woniuxy.woniuhis.util.UUIDUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class RoomServiceImpl implements RoomService {
    @Autowired
    private RoomMapper roomMapper;

    @Override
    public void addRoom(Room room) {
        try {
            if (room.getRoomId() == null) {
                room.setRoomId(UUIDUtil.uuid32());
            }
            roomMapper.insertRoom(room);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RoomServiceException("服务维护中");
        }
    }

    @Override
    public void modifyRoom(Room room) {
        try {
            if (room != null) {
                roomMapper.updateRoom(room);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RoomServiceException("服务维护中");
        }
    }

    @Override
    public void addRoomLastBeds(String roomId, Integer amount) {

    }

    @Override
    public void subRoomLastBeds(String roomId, Integer amount) {

    }

    @Override
    public Room findRoomById(String roomId) {
        try {
            if (!StringUtils.isEmpty(roomId)) {
                return roomMapper.selectRoomById(roomId);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RoomServiceException("服务维护中");
        }
    }

    @Override
    public PageInfo<Room> findRoomListByCondition(Room condition, Integer pageNum, Integer pageSize) {
        try {
            if (pageNum == null) {
                pageNum = 0;
            }
            if (pageSize == null || pageSize <= 0) {
                //默认返回五条数据
                pageSize = 5;
            }
            return new PageInfo<Room>(roomMapper.selectRoomListByCondition(condition, pageNum, pageSize));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RoomServiceException("服务维护中");
        }
    }
}
